home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / Managed / Direct3D / HDRCubeMap / HDRCubeMap.fx < prev    next >
Encoding:
Text File  |  2004-09-27  |  9.4 KB  |  357 lines

  1. //-----------------------------------------------------------------------------
  2. // File: HDRCubeMap.fx
  3. //
  4. // Desc: Effect file for high dynamic range cube mapping sample.
  5. //
  6. // Copyright (c) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8.  
  9. #ifndef MAX_NUM_LIGHTS
  10. #define MAX_NUM_LIGHTS 4
  11. #endif
  12.  
  13.  
  14. float4x4 worldViewMatrix;
  15. float4x4 projMatrix;
  16. texture  cubeMapTexture;
  17. texture  cubeMapTexture2;
  18. texture  sceneTexture;
  19. float4   lightIntensityVector = { 15.0f, 15.0f, 15.0f, 1.0f };
  20. float4   lightPositionView[MAX_NUM_LIGHTS];  // Light positions in view space
  21. float    reflectivity;                  // Reflectivity value
  22.  
  23.  
  24. //-----------------------------------------------------------------------------
  25. // Sampler: samCubeMap
  26. // Desc: Process vertex for HDR environment mapping
  27. //-----------------------------------------------------------------------------
  28. samplerCUBE g_samCubeMap = 
  29. sampler_state
  30. {
  31.     Texture = <cubeMapTexture>;
  32.     MinFilter = Linear;
  33.     MagFilter = Linear;
  34.     MipFilter = Linear;
  35. };
  36.  
  37.  
  38. samplerCUBE g_samCubeMap2 = 
  39. sampler_state
  40. {
  41.     Texture = <cubeMapTexture2>;
  42.     MinFilter = Linear;
  43.     MagFilter = Linear;
  44.     MipFilter = Linear;
  45. };
  46.  
  47.  
  48. sampler2D g_samScene =
  49. sampler_state
  50. {
  51.     Texture = <sceneTexture>;
  52.     MinFilter = Linear;
  53.     MagFilter = Linear;
  54.     MipFilter = Linear;
  55. };
  56.  
  57.  
  58. //-----------------------------------------------------------------------------
  59. // Vertex Shader: HDRVertEnvMap
  60. // Desc: Process vertex for HDR environment mapping
  61. //-----------------------------------------------------------------------------
  62. void HDRVertEnvMap( float4 Pos : POSITION,
  63.                     float3 Normal : NORMAL,
  64.                     out float4 oPos : POSITION,
  65.                     out float3 EnvTex : TEXCOORD0 )
  66. {
  67.     oPos = mul( Pos, worldViewMatrix );
  68.  
  69.     //
  70.     // Compute normal in camera space
  71.     //
  72.     float3 vN = mul( Normal, worldViewMatrix );
  73.     vN = normalize( vN );
  74.  
  75.     //
  76.     // Obtain the reverse eye vector
  77.     //
  78.     float3 vEyeR = -normalize( oPos );
  79.  
  80.     //
  81.     // Compute the reflection vector
  82.     //
  83.     float3 vRef = 2 * dot( vEyeR, vN ) * vN - vEyeR;
  84.  
  85.     //
  86.     // Store the reflection vector in texcoord1
  87.     //
  88.     EnvTex = vRef;
  89.  
  90.     //
  91.     // Apply the projection
  92.     //
  93.     oPos = mul( oPos, projMatrix );
  94. }
  95.  
  96.  
  97. //-----------------------------------------------------------------------------
  98. // Pixel Shader: HDRPixEnvMap
  99. // Desc: Process pixel for HDR environment mapped object
  100. //-----------------------------------------------------------------------------
  101. float4 HDRPixEnvMap( float3 Tex : TEXCOORD0 ) : COLOR
  102. {
  103.     return reflectivity * texCUBE( g_samCubeMap, Tex );
  104. }
  105.  
  106.  
  107. float4 HDRPixEnvMap2Tex( float3 Tex : TEXCOORD0 ) : COLOR
  108. {
  109.     return reflectivity * float4( texCUBE( g_samCubeMap, Tex ).xy, texCUBE( g_samCubeMap2, Tex ).xy );
  110. }
  111.  
  112.  
  113. //-----------------------------------------------------------------------------
  114. // Vertex Shader: HDRVertScene
  115. // Desc: Process vertex for HDR-enabled scene
  116. //-----------------------------------------------------------------------------
  117. void HDRVertScene( float4 iPos : POSITION,
  118.                    float3 iNormal : NORMAL,
  119.                    float2 iTex : TEXCOORD0,
  120.                    out float4 oPos : POSITION,
  121.                    out float2 Tex : TEXCOORD0,
  122.                    out float3 Pos : TEXCOORD1,
  123.                    out float3 Normal : TEXCOORD2 )
  124. {
  125.     //
  126.     // Transform position to view space
  127.     //
  128.     oPos = mul( iPos, worldViewMatrix );
  129.  
  130.     //
  131.     // Also write view position to texcoord1 to do per-pixel lighting
  132.     //
  133.     Pos = oPos;
  134.  
  135.     //
  136.     // Transform to screen coord
  137.     //
  138.     oPos = mul( oPos, projMatrix );
  139.  
  140.     //
  141.     // Transform normal and write to texcoord2 for per-pixel lighting
  142.     //
  143.     Normal = normalize( mul( iNormal, (float3x3)worldViewMatrix ) );
  144.     
  145.     //
  146.     // Propagate texture coord
  147.     //
  148.     Tex = iTex;
  149. }
  150.  
  151.  
  152. //-----------------------------------------------------------------------------
  153. // Pixel Shader: HDRPixScene
  154. // Desc: Process pixel (do per-pixel lighting) for HDR-enabled scene
  155. //-----------------------------------------------------------------------------
  156. float4 HDRPixScene( float2 Tex : TEXCOORD0,
  157.                     float3 Pos : TEXCOORD1,
  158.                     float3 Normal : TEXCOORD2 ) : COLOR
  159. {
  160.     float3 N = normalize( Normal );
  161.  
  162.     // Variable to save lit value by each light
  163.     float4 vPixValue = (float4)0;
  164.  
  165.     //
  166.     // Iterate through each light and apply the light on the pixel
  167.     //
  168.     for( int LightIndex = 0; LightIndex < MAX_NUM_LIGHTS; ++LightIndex )
  169.     {
  170.         //
  171.         // Compute light vector (pixel to light)
  172.         //
  173.         float3 vRLightVec = (float3)(lightPositionView[LightIndex] - Pos);
  174.  
  175.         //
  176.         // Find out the light intensity at the vertex based on
  177.         // N dot L and distance from the light.
  178.         //
  179.         float fDiffuse = saturate( dot( normalize( vRLightVec ), N ) );
  180.  
  181.         //
  182.         // Attenuation is 1 / D^2. Clamp at 1 to avoid infinity.
  183.         //
  184.         float fAttenuation = saturate( 1.0f / dot( vRLightVec, vRLightVec ) );
  185.  
  186.         //
  187.         // Compute and add pixel color to final value
  188.         //
  189.         vPixValue += fDiffuse * fAttenuation;
  190.     }
  191.  
  192.     return tex2D( g_samScene, Tex ) * vPixValue * lightIntensityVector;
  193. }
  194.  
  195.  
  196. float4 HDRPixScene_FirstHalf( float2 Tex : TEXCOORD0,
  197.                               float3 Pos : TEXCOORD1,
  198.                               float3 Normal : TEXCOORD2 ) : COLOR
  199. {
  200.     return HDRPixScene( Tex, Pos, Normal ).xyzw;
  201. }
  202.  
  203.  
  204. float4 HDRPixScene_SecondHalf( float2 Tex : TEXCOORD0,
  205.                                float3 Pos : TEXCOORD1,
  206.                                float3 Normal : TEXCOORD2 ) : COLOR
  207. {
  208.     return HDRPixScene( Tex, Pos, Normal ).zwww;
  209. }
  210.  
  211.  
  212. //-----------------------------------------------------------------------------
  213. // Vertex Shader: HDRVertLight
  214. // Desc: Process vertex for light objects
  215. //-----------------------------------------------------------------------------
  216. void HDRVertLight( float4 iPos : POSITION,
  217.                    out float4 oPos : POSITION,
  218.                    out float4 Diffuse : TEXCOORD1 )
  219. {
  220.     //
  221.     // Transform position to screen space
  222.     //
  223.     oPos = mul( iPos, worldViewMatrix );
  224.     oPos = mul( oPos, projMatrix );
  225.  
  226.     //
  227.     // Diffuse color is the light intensity value
  228.     //
  229.     Diffuse = lightIntensityVector;
  230. }
  231.  
  232.  
  233. //-----------------------------------------------------------------------------
  234. // Pixel Shader: HDRPixLight
  235. // Desc: Process pixel for HDR-enabled scene
  236. //-----------------------------------------------------------------------------
  237. float4 HDRPixLight( float4 Diffuse : TEXCOORD1 ) : COLOR
  238. {
  239.     //
  240.     // Diffuse has the full intensity of the light.
  241.     // Just output it.
  242.     //
  243.     return Diffuse;
  244. }
  245.  
  246.  
  247. float4 HDRPixLight_FirstHalf( float4 Diffuse : TEXCOORD1 ) : COLOR
  248. {
  249.     //
  250.     // Diffuse has the full intensity of the light.
  251.     // Just output it.
  252.     //
  253.     return Diffuse.xyww;
  254. }
  255.  
  256.  
  257. float4 HDRPixLight_SecondHalf( float4 Diffuse : TEXCOORD1 ) : COLOR
  258. {
  259.     //
  260.     // Diffuse has the full intensity of the light.
  261.     // Just output it.
  262.     //
  263.     return Diffuse.zwww;
  264. }
  265.  
  266.  
  267. //-----------------------------------------------------------------------------
  268. // Technique: RenderScene
  269. // Desc: Renders scene objects
  270. //-----------------------------------------------------------------------------
  271. technique RenderScene
  272. {
  273.     pass p0
  274.     {
  275.         VertexShader = compile vs_1_1 HDRVertScene();
  276.         PixelShader = compile ps_2_0 HDRPixScene();
  277.     }
  278. }
  279.  
  280.  
  281. technique RenderSceneFirstHalf
  282. {
  283.     pass p0
  284.     {
  285.         VertexShader = compile vs_1_1 HDRVertScene();
  286.         PixelShader = compile ps_2_0 HDRPixScene_FirstHalf();
  287.     }
  288. }
  289.  
  290.  
  291. technique RenderSceneSecondHalf
  292. {
  293.     pass p0
  294.     {
  295.         VertexShader = compile vs_1_1 HDRVertScene();
  296.         PixelShader = compile ps_2_0 HDRPixScene_SecondHalf();
  297.     }
  298. }
  299.  
  300.  
  301. //-----------------------------------------------------------------------------
  302. // Technique: RenderLight
  303. // Desc: Renders light objects
  304. //-----------------------------------------------------------------------------
  305. technique RenderLight
  306. {
  307.     pass p0
  308.     {
  309.         VertexShader = compile vs_1_1 HDRVertLight();
  310.         PixelShader = compile ps_2_0 HDRPixLight();
  311.     }
  312. }
  313.  
  314.  
  315. technique RenderLightFirstHalf
  316. {
  317.     pass p0
  318.     {
  319.         VertexShader = compile vs_1_1 HDRVertLight();
  320.         PixelShader = compile ps_2_0 HDRPixLight_FirstHalf();
  321.     }
  322. }
  323.  
  324.  
  325. technique RenderLightSecondHalf
  326. {
  327.     pass p0
  328.     {
  329.         VertexShader = compile vs_1_1 HDRVertLight();
  330.         PixelShader = compile ps_2_0 HDRPixLight_SecondHalf();
  331.     }
  332. }
  333.  
  334.  
  335. //-----------------------------------------------------------------------------
  336. // Technique: RenderEnvMesh
  337. // Desc: Renders the HDR environment-mapped mesh
  338. //-----------------------------------------------------------------------------
  339. technique RenderHDREnvMap
  340. {
  341.     pass p0
  342.     {
  343.         VertexShader = compile vs_1_1 HDRVertEnvMap();
  344.         PixelShader = compile ps_2_0 HDRPixEnvMap();
  345.     }
  346. }
  347.  
  348.  
  349. technique RenderHDREnvMap2Tex
  350. {
  351.     pass p0
  352.     {
  353.         VertexShader = compile vs_1_1 HDRVertEnvMap();
  354.         PixelShader = compile ps_2_0 HDRPixEnvMap2Tex();
  355.     }
  356. }
  357.